function fig = fm_simset(varargin)
% FM_SIMSET create GUI for Simulink model settings
%
% HDL = FM_SIMSET()
%
%Author:    Federico Milano
%Date:      11-Nov-2002
%Version:   1.0.0
%
%E-mail:    Federico.Milano@uclm.es
%Web-site:  http://www.uclm.es/area/gsee/Web/Federico
%
% Copyright (C) 2002-2013 Federico Milano

global File Settings Path Theme Fig Path

if nargin
  if ~exist('Fig'), Fig.simset = 0, end
  if ~Fig.simset
    Fig.simset = findobj(get(0,'Children'),'FileName','fm_simset');
  end
  switch varargin{1}

   case 'noarrows'

    Settings.noarrows = ~Settings.noarrows;
    if Settings.noarrows,
      set(gcbo,'Checked','on')
    else
      set(gcbo,'Checked','off')
    end

   case 'buttondown'

    pos = get(Fig.simset,'CurrentPoint');
    if pos(1) > 0.118 && pos(2) > 0.35 && pos(1) < 0.318 && pos(2) < 0.9
      fm_simrep('ViewModel',0,0)
    end

   case 'buttonmotion'

    if Settings.hostver >= 7.01
      pointer = 'hand';
    else
      pointer = 'crosshair';
    end

    pos = get(Fig.simset,'CurrentPoint');
    if pos(1) > 0.118 && pos(2) > 0.35 && pos(1) < 0.318 && pos(2) < 0.9
      set(Fig.simset,'Pointer',pointer);
    else
      set(Fig.simset,'Pointer','arrow');
    end

  end
  return
end

if Fig.simset, figure(Fig.simset), return, end

if strcmp(Settings.platform,'MAC')
  dm = 0.035;
else
  dm = 0;
end

h0 = figure('Color',Theme.color01, ...
  'Units','normalized', ...
  'Colormap',[], ...
  'CreateFcn','Fig.simset = gcf;', ...
  'DeleteFcn','Fig.simset = 0;', ...
  'FileName','fm_simset', ...
  'MenuBar','none', ...
  'Name','Simulink Settings', ...
  'NumberTitle','off', ...
  'PaperPosition',[18 180 576 432], ...
  'PaperUnits','points', ...
  'Position',sizefig(0.3818,0.2721), ...
  'ToolBar','none', ...
  'WindowButtonDownFcn','fm_simset buttondown', ...
  'WindowButtonMotionFcn','fm_simset buttonmotion');

h1 = uimenu('Parent',h0, ...
  'Label','File', ...
  'Tag','U2');
h2 = uimenu('Parent',h1, ...
  'Callback','fm_simrep(''mdl2eps'',0,0)', ...
  'Label','Export network to color EPS', ...
  'Tag','S21e', ...
  'Accelerator','e');
h2 = uimenu('Parent',h1, ...
  'Callback','fm_simrep(''mdl2eps'',1,0)', ...
  'Label','Export network to grey EPS', ...
  'Tag','S21g', ...
  'Accelerator','g');
h2 = uimenu('Parent',h1, ...
  'Callback',['try, close_system(''',File.data(1:end-5),''',1), end'], ...
  'Label','Close & Save Network', ...
  'Tag','S21', ...
  'Separator','on', ...
  'Accelerator','s');
h2 = uimenu('Parent',h1, ...
  'Callback',['try, close_system(''',File.data(1:end-5),''',0), end'], ...
  'Label','Close without saving', ...
  'Tag','S21', ...
  'Accelerator','c');
h2 = uimenu('Parent',h1, ...
  'Callback','close(gcf);', ...
  'Label','Exit', ...
  'Tag','S22', ...
  'Accelerator','x');

h1 = uimenu('Parent',h0, ...
  'Label','Edit', ...
  'Tag','U1');

% Font Type
h2 = uimenu('Parent',h1, ...
  'Label','Font Type', ...
  'Tag','S11');
versione = version;
if strcmp(versione(1),'6')
  c = listfonts;
else
  c = {'courier';'helvetica';'times'};
end
if isempty(c{1}), c(1) = []; end
n = fix(length(c)/25)+sign(rem(length(c),25));

for j = 1:n
  for i = 1:min(25,length(c)-25*(j-1))
    cb = ['fm_simrep(''FontType'',0,''',c{i+(j-1)*25},''')'];
    tb = ['Font',num2str(i)];
    h3 = uimenu('Parent',h2, ...
      'Callback',cb, ...
      'Label',c{i+(j-1)*25}, ...
      'Tag',tb);
  end
  if length(c)-25*(j-1) > 25,
    h3 = uimenu('Parent',h2, ...
      'Label','more ...', ...
      'Tag',tb);
  end
  h2 = h3;
end

% Font Size
h2 = uimenu('Parent',h1, ...
  'Label','Font Size', ...
  'Tag','S12');
n = 4:2:18;
for i = 1:length(n)
  sn = num2str(n(i));
  cb = ['fm_simrep(''FontSize'',',sn,',0)'];
  tb = ['Size',sn];
  h3 = uimenu('Parent',h2, ...
    'Callback',cb, ...
    'Label',sn, ...
    'Tag',tb);
end

h2 = uimenu('Parent',h1, ...
            'Separator','on', ...
            'Callback','fm_simset noarrows', ...
            'Label','Remove arrows from EPS', ...
            'Tag','S11');
if Settings.noarrows,
  set(h2,'Checked','on'),
else,
  set(h2,'Checked','off'),
end

h1 = axes('Parent',h0, ...
  'Box','on', ...
  'CameraUpVector',[0 1 0], ...
  'CameraUpVectorMode','manual', ...
  'Color',Theme.color04, ...
  'ColorOrder',Settings.color, ...
  'Layer','top', ...
  'Position',[0.118 0.35 0.2 0.55], ...
  'Tag','Axes1', ...
  'XColor',Theme.color05, ...
  'XLim',[0.5 80.5], ...
  'XLimMode','manual', ...
  'XTickMode','manual', ...
  'YColor',Theme.color05, ...
  'YDir','reverse', ...
  'YLim',[0.5 116.5], ...
  'YLimMode','manual', ...
  'YTickMode','manual', ...
  'ZColor',Theme.color05);
h2 = image('Parent',h1, ...
  'CData',imread([Path.images,'logo_simulink.jpg'],'jpg'), ...
  'Tag','Axes1Image1', ...
  'XData',[1 80], ...
  'YData',[1 116]);

h1 = uicontrol('Parent',h0, ...
  'Units','normalized', ...
  'CData',imread([Path.images,'sims_busr.bmp'],'bmp'), ...
  'BackgroundColor',Theme.color02, ...
  'Callback','fm_simrep(''VoltageReport'',0,0)', ...
  'Position',[0.4388    0.6886    0.2344    0.2030+dm], ...
  'TooltipString','Bus Report', ...
  'Tag','Pushbutton1');
h1 = uicontrol('Parent',h0, ...
  'Units','normalized', ...
  'CData',imread([Path.images,'sims_busw.bmp'],'bmp'), ...
  'BackgroundColor',Theme.color02, ...
  'Callback','fm_simrep(''WipeVoltageReport'',0,0)', ...
  'Position',[0.6919    0.6886    0.2344    0.2030+dm], ...
  'TooltipString','Wipe Bus Report', ...
  'Tag','Pushbutton1');
h1 = uicontrol('Parent',h0, ...
  'Units','normalized', ...
  'CData',imread([Path.images,'sims_liner.bmp'],'bmp'), ...
  'BackgroundColor',Theme.color02, ...
  'Callback','fm_simrep(''PowerFlowReport'',0,0)', ...
  'Position',[0.4388    0.4856    0.2344    0.2030+dm], ...
  'TooltipString','Power Flows', ...
  'Tag','Pushbutton1');
h1 = uicontrol('Parent',h0, ...
  'Units','normalized', ...
  'CData',imread([Path.images,'sims_linew.bmp'],'bmp'), ...
  'BackgroundColor',Theme.color02, ...
  'Callback','fm_simrep(''WipePowerFlowReport'',0,0)', ...
  'Position',[0.6919    0.4856    0.2344    0.2030+dm], ...
  'TooltipString','Wipe Power Flows', ...
  'Tag','Pushbutton1');
h1 = uicontrol('Parent',h0, ...
  'Units','normalized', ...
  'CData',imread([Path.images,'sims_names.bmp'],'bmp'), ...
  'BackgroundColor',Theme.color02, ...
  'Callback','fm_simrep(''ShowNames'',0,0)', ...
  'Position',[0.4388    0.2826    0.2344    0.2030+dm], ...
  'TooltipString','Show Names', ...
  'Tag','Pushbutton1');
h1 = uicontrol('Parent',h0, ...
  'Units','normalized', ...
  'CData',imread([Path.images,'sims_nameh.bmp'],'bmp'), ...
  'BackgroundColor',Theme.color02, ...
  'Callback','fm_simrep(''HideNames'',0,0)', ...
  'Position',[0.6919    0.2826    0.2344    0.2030+dm], ...
  'TooltipString','Hide Names', ...
  'Tag','Pushbutton1');
h1 = uicontrol('Parent',h0, ...
  'Units','normalized', ...
  'CData',imread([Path.images,'sims_busn.bmp'],'bmp'), ...
  'BackgroundColor',Theme.color02, ...
  'Callback','fm_simrep(''ShowBusNames'',0,0)', ...
  'Position',[0.4388    0.0796    0.2344    0.2030+dm], ...
  'TooltipString','Show Bus Names', ...
  'Tag','Pushbutton1');
h1 = uicontrol('Parent',h0, ...
  'Units','normalized', ...
  'CData',imread([Path.images,'sims_busv.bmp'],'bmp'), ...
  'BackgroundColor',Theme.color02, ...
  'Callback','fm_simrep(''HideBusNames'',0,0)', ...
  'Position',[0.6919    0.0796    0.2344    0.2030+dm], ...
  'TooltipString','Hide Bus Names', ...
  'Tag','Pushbutton1');

h1 = uicontrol( ...
    'Parent',h0, ...
    'Units','normalized', ...
    'BackgroundColor',Theme.color03, ...
    'ForegroundColor',Theme.color09, ...
    'Callback','close(gcf)', ...
    'String','OK', ...
    'FontWeight','bold', ...
    'Position',[0.1    0.0796    0.2344   0.1500+dm], ...
    'Tag','Pushbutton1');

if nargout > 0, fig = h0; end